import {Injectable, signal} from '@angular/core';

export interface Area {
  id: string;
  name: string;
  x: number;
  y: number;
  width: number;
  height: number;
  color: string;
}

@Injectable({
  providedIn: 'root'
})
export class AreasService {
  private _areas = signal<Area[]>([]);

  readonly areas = this._areas.asReadonly();

  addArea(area: Area) {
    this._areas.update(areas => [...areas, area]);
  }

  updateArea(id: string, updates: Partial<Area>) {
    this._areas.update(areas =>
      areas.map(area =>
        area.id === id ? {...area, ...updates} : area
      )
    );
  }

  removeArea(id: string) {
    this._areas.update(areas => areas.filter(area => area.id !== id));
  }

  clearAreas() {
    this._areas.set([]);
  }
}
